#include<classify.h>
#include<clutil.h>
#include<assert.h>
#include<bitset>
#include<string.h>
#include<Timer.h>

#define SIZE 16384
void test()
{
    const unsigned size = SIZE * sizeof(unsigned);
    unsigned* data = (unsigned*)allocateHostMemory(size);
    cl_mem dData = allocateDeviceMemory(data, size, CL_MEM_READ_WRITE); 
    compileProgram(0, "kernel.cl");
#if 0
    createKernel("test");
    setKernelArg("test", 0, sizeof(cl_mem), &dData);

	size_t szLocalWorkSize = 256;
	size_t szGlobalWorkSize = SIZE;
    Timer t;
    t.Restart();
    for (unsigned i = 0; i < 10; ++i) {
	    runKernel("test", szLocalWorkSize, szGlobalWorkSize);  
        waitForEvent();
    }
    cout << "Time taken: " << t.GetTime() << " seconds" << endl;
    copyFromDevice(dData, data, size);
    for (unsigned i = 0; i < SIZE; ++i) {
       assert(data[i] == 10);
    }
    cout << "Verified" << endl;
#endif
    
    unsigned* synch = (unsigned*)allocateHostMemory(sizeof(unsigned));
    
    *synch = 0;
    cl_mem dSynch = allocateDeviceMemory(synch, sizeof(unsigned), CL_MEM_READ_WRITE);
    createKernel("testA");
    setKernelArg("testA", 0, sizeof(cl_mem), &dData);
    setKernelArg("testA", 1, sizeof(cl_mem), &dSynch);

    size_t szLocalWorkSize = 256;
    size_t szGlobalWorkSize = SIZE;
    Timer t;
    t.Restart();
    runKernel("testA", szLocalWorkSize, szGlobalWorkSize);
    waitForEvent();
    cout << "Time taken: " << t.GetTime() << " seconds" << endl;
    copyFromDevice(dData, data, size);
    for (unsigned i = 0; i < SIZE; ++i) {
       assert(data[i] == 10);
    }
    cout << "Verified" << endl; 

    cleanup();

}

void
classify(const char* file)
{
    fileRead(file);
    initExecution(); 
    test(); 
#if 0
    const unsigned dataSize = nInstances * nAttributes * sizeof(unsigned);
    cl_mem dDatabase = allocateDeviceMemory(dataBase, dataSize, CL_MEM_READ_ONLY);
    compileProgram(0, "kernel.cl"); 
  
    createKernel("classify");
 
    setKernelArg("classify", 0, sizeof(cl_mem), &dDatabase);
    setKernelArg("classify", 1, sizeof(unsigned), &nInstances);
    setKernelArg("classify", 0, sizeof(unsigned), &nAttributes);

    size_t szLocalWorkSize = NO_THREADS_BLOCK;
    size_t szGlobalWorkSize = nAttributes;

    runKernel("classify", szLocalWorkSize, szGlobalWorkSize); 
    waitForEvent();
#endif
}



